package com.gcxx.framework.security.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gcxx.framework.base.entity.PageBean;
import com.gcxx.framework.base.service.BaseService;
import com.gcxx.framework.security.dao.IFrsfGroupDAO;
import com.gcxx.framework.security.dao.IFrsfGroupRoleDAO;
import com.gcxx.framework.security.dao.IFrsfUserGroupDAO;
import com.gcxx.framework.security.entity.FrsfGroup;
import com.gcxx.framework.security.entity.FrsfGroupRole;
import com.gcxx.framework.security.entity.FrsfUserGroup;
import com.gcxx.framework.security.service.IGroupService;
import com.gcxx.framework.security.service.IUserService;
import com.gcxx.framework.util.validate.ValidateUtil;

/**
 * <strong>Title : IGroupService<br></strong>
 * <strong>Description : </strong>群组信息接口实现<br> 
 * <strong>Create on : 2013-2-23<br></strong>
 * <p>
 * <strong>Copyright (C) <br></strong>
 * <p>
 * @author yanghailong<br>
 * @version <strong>GCXX</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人		修改日期		修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public class GroupServiceImpl extends BaseService implements IGroupService {
	
	private static final Log log = LogFactory.getLog(GroupServiceImpl.class);
	
	/**
	 * <code>frsfGroupDAO</code>-群组信息DAO
	 */
	private IFrsfGroupDAO frsfGroupDAO;
	private IFrsfUserGroupDAO frsfUserGroupDAO;
	private IFrsfGroupRoleDAO frsfGroupRoleDAO;

	public IFrsfGroupDAO getFrsfGroupDAO() {
		return frsfGroupDAO;
	}

	public void setFrsfGroupDAO(IFrsfGroupDAO frsfGroupDAO) {
		this.frsfGroupDAO = frsfGroupDAO;
	}

	public IFrsfUserGroupDAO getFrsfUserGroupDAO() {
		return frsfUserGroupDAO;
	}

	public void setFrsfUserGroupDAO(IFrsfUserGroupDAO frsfUserGroupDAO) {
		this.frsfUserGroupDAO = frsfUserGroupDAO;
	}

	public IFrsfGroupRoleDAO getFrsfGroupRoleDAO() {
		return frsfGroupRoleDAO;
	}

	public void setFrsfGroupRoleDAO(IFrsfGroupRoleDAO frsfGroupRoleDAO) {
		this.frsfGroupRoleDAO = frsfGroupRoleDAO;
	}

	@Override
	public List<FrsfGroup> findAllGroup() {
		return frsfGroupDAO.findAll();
	}

	@Override
	public void save(FrsfGroup entity) {
		if(entity.getId()==null||"".equals(entity.getId())){
			entity.setId(getID(IGroupService.FRS_GROUP));
			frsfGroupDAO.save(entity);
		}else{
			frsfGroupDAO.attachDirty(entity);
		}
	}
	@Override
	public void saveOnly(FrsfGroup entity) {
		frsfGroupDAO.save(entity);
	}
	@Override
	public void delete(FrsfGroup entity) {
		frsfGroupDAO.delete(entity);
	}
	@Override
	public void deleteByGroups(List entitys) {
		frsfGroupDAO.deleteByIds(entitys);
	}

	@Override
	public FrsfGroup findById(String id) {
		FrsfGroup entity =  frsfGroupDAO.findById(id);
		return entity;
	}
	@Override
	public List<FrsfGroup> findByExample(FrsfGroup entity){
		return frsfGroupDAO.findByExample(entity);
	}
	
	@Override
	public List findByProperty(String propertyName, Object value) {
		return frsfGroupDAO.findByProperty(propertyName, value);
	}

	@Override
	public Map findByCondition(Map condition, String[] order, int index, int pageSize) {
		return frsfGroupDAO.findByCondition( condition, order, index, pageSize);
	}

	/*
	 * (non-Javadoc)
	 * @see com.gcxx.framework.security.service.IGroupService#findFlowRoleByCondition(java.util.Map, java.lang.String[], int, int, java.lang.String)
	 */
	public Map findFlowRoleByCondition(Map condition, String[] order, int index, int pageSize,String groupId){
		return frsfGroupDAO.findFlowRoleByCondition( condition, order, index, pageSize,groupId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.gcxx.framework.security.service.IGroupService#findNotInGroupUsers(java.util.Map, java.lang.String[], int, int)
	 */
	public Map findNotInGroupUsers( Map condition, String[] order,int index,int pageSize){
		return frsfGroupDAO.findNotInGroupUsers( condition, order, index, pageSize);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.gcxx.framework.security.service.IGroupService#deleteGroupUsers(java.lang.String, java.util.List)
	 */
	public void deleteGroupUsers( String groupId,String[] userIds){
		//删除群组指定用户信息
		if(userIds!=null&&!"".equals(userIds)){
			for(String userId :userIds){
				FrsfUserGroup userGroup=new FrsfUserGroup();
				userGroup.setGroupId(groupId);
				userGroup.setUserId(userId);
				List <FrsfUserGroup>  entity =frsfUserGroupDAO.findByExample(userGroup);
				if(ValidateUtil.isNotEmpty(entity)&&entity.size()>0){
			        frsfUserGroupDAO.delete(entity.get(0));
				}
			}	
		}
	}
	

    /*
     * (non-Javadoc)
     * @see com.gcxx.framework.security.service.IGroupService#addGroupUsers(java.lang.String, java.lang.String[])
     */
	public void addGroupUsers( String groupId,String[] userIds){
		for(String userId :userIds){
			FrsfUserGroup userGroup=new FrsfUserGroup();
			userGroup.setId(getID(IUserService.FRS_USER_GROUP));
			userGroup.setGroupId(groupId);
			userGroup.setUserId(userId);
			frsfUserGroupDAO.save(userGroup);
		}
	}
	@Override
	public List<FrsfGroup>  findNameByNId(FrsfGroup entity){
		return frsfGroupDAO.findNameByNId(entity);
	}
	
	@Override
	public void  saveGroupRole(String id,String [] roleIds){
		//删除群组权限信息
		List<FrsfGroupRole> entitys = frsfGroupRoleDAO.findByProperty("groupId", id);
		if(ValidateUtil.isNotEmpty(entitys)){
			for (FrsfGroupRole groupRole : entitys) {
		        frsfGroupRoleDAO.delete(groupRole);
			}
		}
		//保存群组权限
		for(String roleId :roleIds){
			FrsfGroupRole entity= new FrsfGroupRole();
			//回滚测试使用
//			id=null;
			entity.setGroupId(id);
			entity.setRoleId(roleId);
			entity.setId(getID(IGroupService.FRS_GROUP_ROLE));
			frsfGroupRoleDAO.save(entity);
		}

	}
	@Override
	public void  saveGroupUsers(String id,String [] userIds){
		//删除群组用户信息
		List<FrsfUserGroup> entitys=   frsfUserGroupDAO.findByProperty("groupId", id);
		if(ValidateUtil.isNotEmpty(entitys)){
			for (FrsfUserGroup userGroup : entitys) {
		        frsfUserGroupDAO.delete(userGroup);
			}
		}
		//保存用户所属群组
		for(String userId :userIds){
			FrsfUserGroup entity= new FrsfUserGroup();
			entity.setGroupId(id);
			entity.setUserId(userId);
			entity.setId(getID(IUserService.FRS_USER_GROUP));
			frsfUserGroupDAO.save(entity);
		}
	}

	/* (non-Javadoc)
	 * @see com.gcxx.framework.security.service.IGroupService#findByConditionForHospital(com.gcxx.framework.base.entity.PageBean)
	 */
	@Override
	public PageBean findByConditionForHospital(PageBean page) {
		// TODO Auto-generated method stub
		return frsfGroupDAO.findByConditionForHospital(page);
	}

	@Override
	public void updateRoleByEntity(FrsfGroup frsfGroup) throws Exception {
		try {
			frsfGroupDAO.merge(frsfGroup);
		} catch (Exception e) {
			log.error(e);
			throw e;
		}
		
	}
}
